home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / otmvoxel / otmvoxel.txt < prev   
Text File  |  1994-11-08  |  9KB  |  226 lines

  1.         otmvoxel released 11-08-94
  2.  
  3.         voxel landscape explanation/demo
  4.         by Voltaire/OTM
  5.         all source Copyright (C) 1994 Zach Mortensen
  6.  
  7.         email -
  8.         mortens1@nersc.gov
  9.  
  10.         see OTMVOXEL.NFO and OTM-94.NFO for more information
  11.  
  12.  
  13. OPENING WORDS
  14.  
  15.         I make the assumption that you have at least some experience in
  16.         writing 3d code.  You should not attempt to understand voxels if you
  17.         cannot understand the basics of 3d.  If you are interested in the 3d
  18.         engine used to make this demo, it is availible via ftp at
  19.  
  20.         hornet.eng.ufl.edu
  21.  
  22.         the archive is
  23.  
  24.         /demos/code/graph/library/V3DT090.ZIP
  25.  
  26.         I highly reccomend picking it up (unbiased opinion of the author ;))
  27.  
  28.  
  29.  
  30. WHAT ARE VOXELS?
  31.  
  32.         A voxel is an approximation of volume, much in the same way a pixel
  33.         is an approximation for area.  Imagine a voxel as a cube in space,
  34.         it has length, width and depth.  Just pixels have a fixed length and
  35.         width, voxels have a fixed length, width, and depth.
  36.  
  37.          ---------
  38.         |\        \
  39.         | \        \
  40.         |  \        \
  41.         |   ---------       ---------
  42.         |   |       |       |       |
  43.         |   |       |       |       |
  44.         |   | voxel |       | pixel |
  45.          \  |       |       |       |
  46.           \ |       |       |       |
  47.            \|_______|       ---------
  48.  
  49.         gotta love my ascii art...heheh
  50.  
  51.         Now that you know what voxels are...
  52.  
  53.  
  54. HOW ARE VOXELS USED?
  55.  
  56.         Because they can approximate volume, voxels can be used to create
  57.         very complex formations in three dimensions.  Commanche made
  58.         voxels famous for creating somewhat realistic landscapes in
  59.         realtime.  Many recent demos have also made use of voxels in
  60.         landscapes, ocean waves, etc.
  61.  
  62.         But how do they do it so quickly?  It would seem that in order
  63.         to draw a voxel, one must keep track of its verticies, rotate
  64.         and translate them, and draw a cube in every frame.  This is
  65.         obviously too slow to do in realtime, because landscapes usually
  66.         contain upwards of 1000 voxels.
  67.  
  68.         Here's the quick and dirty shortcut:  If we limit our perspective
  69.         so that we only view voxels from the front, each cube looks like
  70.         this:
  71.  
  72.                 ---------
  73.                 |       |
  74.                 |       |
  75.                 | voxel |
  76.                 |       |
  77.                 |       |
  78.                 ---------
  79.  
  80.         Wow...that looks an awful lot like a square, and we can draw squares
  81.         MUCH faster than we can draw polygons with arbitrary angles that
  82.         are required to draw a cube in 3d.  More importantly, we only need
  83.         to keep track of one point.
  84.  
  85.            P1
  86.               \
  87.                 x--------
  88.                 |       |
  89.                 |       |
  90.                 | voxel |
  91.                 |       |
  92.                 |       |
  93.                 ---------
  94.  
  95.  
  96.  
  97. SO WHAT DO WE DO NOW?
  98.  
  99.         Well, now that we know voxels can be approximated by a point and
  100.         a square, we need to define a pattern for our voxels that looks
  101.         somewhat like a landscape.  It's best to start out simple, by
  102.         defining a parametric curve in 3d such as
  103.  
  104.         x = 10t
  105.         y = 20(sin x + sin z) + 25
  106.         z = 10t
  107.  
  108.         make sure you convert x and z to radians before you take their sine
  109.         when you are calculating the y value.  These equations form something
  110.         that vaguely resembles a hill.  Make a 10x10 array of points, 
  111.         assign a voxel to each point, and you can rotate your hill, translate 
  112.         it, etc.  Several problems soon become evident, though.  First, our 
  113.         hill seems to have some unsightly holes in it.  This is due to the 
  114.         fact that we have attempted to project a voxel without changing its 
  115.         size.  When a voxel is closer to the viewer, it must be larger.  Of 
  116.         course, it must also appear to be smaller when it is far away.  
  117.         Remember to scale your voxels to a size determined by their distance 
  118.         from the viewer.  This is a simple 3d projection, the same way 2d 
  119.         screen coordinates are determined from 3d world coordinates.
  120.                                
  121.         newSize = SIZE * DIST / z3d
  122.  
  123.         where SIZE is the set size for a voxel and DIST is the distance      
  124.         between the viewer and the screen.  I like to use SIZE = 16 and DIST = 
  125.         1024.  Of course, your landscape will look better if you make your 
  126.         voxels smaller and closer together.
  127.  
  128.         Another obvious problem is that our hill appears as a blob of one
  129.         color.  In order to get around this, we need to shade our voxels
  130.         in some inventive way.
  131.  
  132.  
  133. MADE IN THE SHADE
  134.  
  135.         this is perhaps the easiest way to shade a voxel:
  136.  
  137.            P1
  138.               \
  139.                 x--------
  140.                 |5555555|
  141.                 |4444444|
  142.                 |3333333|
  143.                 |2222222|
  144.                 |1111111|
  145.                 ---------
  146.  
  147.         If we assign the the y value of each voxel's point to the starting
  148.         color value (represented above by 5), we get a hill that is smoothly
  149.         shaded from top to bottom.  Of course, this requires a carefully
  150.         chosen palette, but I'll leave that up to you.  Some popular ideas
  151.         include shading the palette from blue (low colors = water) to green
  152.         (middle colors = land) to white (upper colors = snowcapped mountains).
  153.         I use a grossly simple green to white gradient.
  154.  
  155.         Other methods of shading (I haven't tried these, but they seem like
  156.         they would work):
  157.  
  158.                 ---------
  159.                 |1234567|
  160.                 |1234567|
  161.                 |1234567|   (light appears to be coming from one side)
  162.                 |1234567|
  163.                 |1234567|
  164.                 ---------
  165.  
  166.                 ---------
  167.                 |1234567|   (If I'm not mistaken, this is the type of shading
  168.                 |2345678|   that was used in Mars)
  169.                 |3456789|
  170.                 |456789A|
  171.                 |56789AB|
  172.                 ---------
  173.  
  174.         give these a try and see what happens...
  175.  
  176.  
  177. LIMITATIONS OF SHADING
  178.  
  179.         Although shading makes our hill look much better, it does limit the
  180.         way in which we can use our landscape.  If we use this easy method of 
  181.         shading, we cannot rotate our landscape around any axis but the y.  
  182.         Rotation around the y axis does not change the y coordinate of a 
  183.         point, which is the basis for this shading technique.  The shading 
  184.         routine we use simply assumes that color will change in the vertical 
  185.         direction.  By rotating the vertical direction, (rotating around the 
  186.         x or z axis), we make the shading appear incorrect.
  187.  
  188.  
  189. ONWARD AND UPWARD
  190.         
  191.         Now that we have a disgustingly simple landscape working properly,
  192.         we are ready to move on to bigger and better things.  First, we
  193.         must find an equation that will create a more exciting landscape.
  194.         I have found that plasmas and certain fractals are best suited for
  195.         this purpose.  Extruding a plasma or fractal into a landscape
  196.         is an easy process:
  197.  
  198.         for every point (x,y,color) in the plasma or fractal image, create a
  199.         3d point (x,color,y) in the landscape.
  200.  
  201.         This produces some truly incredible results.  Plasmas are spectacular
  202.         with their hills and valleys, and some complex canyon landscapes can
  203.         be obtained by using a section of the Mandelbrot set.  Feel
  204.         free to experiment with any other of your favorite fractals as well.
  205.  
  206.         One thing that is an absolute necessity is that you depth sort your
  207.         points from back to front.  If you do not, your landscape will look
  208.         VERY bad.
  209.  
  210.         Now that we have a potentially HUGE landscape, we must also decide
  211.         where to stop displaying voxels.  This is extremely simple, just weed
  212.         out points that are too far away while you are sorting.  It's up to
  213.         you to decide how far you want your landscape to extend.
  214.  
  215.  
  216. FINAL WORDS
  217.  
  218.         I honestly hope this explanation along with the example code helps to
  219.         clarify the uses of voxels.  If for some reason my explanation seems
  220.         vague in any way, feel free to email me with any questions.  I do not
  221.         claim to be the world's most talented coder, and I realize that I make
  222.         my share of mistakes.  Just make sure you point them out to me ;)
  223.  
  224.  
  225. Volt
  226.